import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../model/chat/purpose.dart';
import '../sdk_service.dart';

class PurposeService extends GetConnect {
  final path = "/cloud-digital-healing/sse/getPurpose";

  final host = Get.find<SDKService>().host;

  PurposeService() : super(timeout: 20.seconds);

  Future<Purpose?> ask({
    required String message,
    required String conversationID,
  }) async {
    final param = {"chatId": conversationID, "content": message};
    final urlStr = "$host$path";
    final resp = await get(urlStr, query: param);
    if (!resp.isOk) {
      return Future.error("Error: ${resp.statusText} in PurposeService.ask");
    }
    //
    final json = resp.body;

    final code = json["code"];
    debugPrint("askPurpose RESP: $json");

    if (code != 1) {
      return Future.error(json["msg"]);
    }
    final resultJSON = json["data"] as Map<String, dynamic>;
    if (resultJSON.isEmpty) {
      return null;
    }

    Purpose? result;
    result = Purpose.fromJson(resultJSON);
    return result;
  }
}
